﻿<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5英文字母悬停翻转动画特效 - 站长素材</title>

<style>
:root {
  --bg-color: seagreen;
  --primary-clr: black;
  --secondary-clr: white;
  --base-ff: 'Source Sans Pro', sans-serif;
  --h2-fs: 2.25rem;
  --pad: 2rem;
}

* {
  box-sizing: border-box;
}

.grid {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  min-height: 100vh;
  font-family: var(--base-ff);
}
.grid__item {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 100%;
  width: 50vw;
  height: 50vh;
  padding: var(--pad);
  background-color: var(--bg-color);
}
@media (min-width: 600px) {
  .grid__item {
    flex: 1 0 50%;
  }
}
.grid__item:nth-child(even) {
  background-color: #36a265;
}
.grid__item:nth-child(3n) {
  background-color: #2b8352;
}
.grid__item:nth-child(4n) {
  background-color: #38aa6a;
}
.grid__item:nth-child(5n) {
  background-color: #267449;
}

h2 {
  font-size: var(--h2-fs);
  text-align: center;
  color: var(--primary-clr);
}

.letter-wrap {
  display: inline-block;
  position: relative;
}

.letter-wrap * {
  pointer-events: none;
}

.letter-wrap__word {
  display: inline-flex;
  position: relative;
  overflow: hidden;
}

.letter-wrap__char {
  display: inline-block;
  position: relative;
  text-transform: uppercase;
}
.letter-wrap__char-inner {
  display: inline-block;
}
.letter-wrap__char-inner:after {
  content: attr(data-letter);
  position: absolute;
  top: 100%;
  left: 0;
  color: var(--secondary-clr);
}
</style>
</head>
<body>

<script src="js/TweenMax.min.js"></script>

<ul class="grid">
  <li class="grid__item">
    <h2 class="letter-wrap">Hello world</h2>
  </li>
  <li class="grid__item">
    <h2 class="letter-wrap" data-duration="0.4" data-stagger="0.1">Good morning</h2>
  </li>
  <li class="grid__item">
    <h2 class="letter-wrap" data-duration="0.2" data-stagger="-0.05">Great days are coming</h2>
  </li>
  <li class="grid__item">
    <h2 class="letter-wrap" data-duration="0.1" data-stagger="0.1">Live for tomorrow</h2>
  </li>
  <li class="grid__item">
    <h2 class="letter-wrap" data-stagger="-0.015">Roll with the punches</h2>
  </li>
  <li class="grid__item">
    <h2 class="letter-wrap" data-duration="1" data-stagger="0.025">Have a blast</h2>
  </li>
  <li class="grid__item">
    <h2 class="letter-wrap" data-duration="0.8" data-stagger="-0.025">Bet on black</h2>
  </li>
  <li class="grid__item">
    <h2 class="letter-wrap" data-duration="0.1" data-stagger="-0.075">Tuck and roll</h2>
  </li>
</ul>

<script>
const letterWrapClass = 'letter-wrap';
const letterWrapElements = document.getElementsByClassName(letterWrapClass);

[...letterWrapElements].forEach(el => {
  letterWrap(el, letterWrapClass);
  letterAnimation(el, letterWrapClass);
});



function letterWrap(el, cls) {
  const words = el.textContent.split(' ');
  const letters = [];
  
  cls = cls || 'letter-wrap'
  
  words.forEach(word => {
    let html = '';
    for (var letter in word) {
      html += `
        <span class="${cls}__char">
          <span class="${cls}__char-inner" data-letter="${word[letter]}">
            ${word[letter]}
          </span>
        </span>
      `;
    };
    
    let wrappedWords = `<span class="${cls}__word">${html}</span>`;
    letters.push(wrappedWords);
  });
  
  return el.innerHTML = letters.join(' ');
}

function letterAnimation(el, cls) {
  const tl = new TimelineMax({ paused: true });
  const characters = el.querySelectorAll(`.${cls}__char-inner`);
  const duration = el.hasAttribute('data-duration') ? el.dataset.duration : 0.3;
  const stagger = el.hasAttribute('data-stagger') ? el.dataset.stagger : 0.03;
  
  el.animation = tl.staggerTo(characters, duration, {
    y: '-100%',
    ease: Power4.easeOut
  }, stagger);
      
  el.addEventListener('mouseenter', (event) => event.currentTarget.animation.play());
  el.addEventListener('mouseout', (event) => el.animation.reverse());
}
</script>

<div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
<p>适用浏览器：360、FireFox、Chrome、Opera、傲游、搜狗、世界之窗. 不支持Safari、IE8及以下浏览器。</p>
<p>来源：<a href="http://sc.chinaz.com/" target="_blank">站长素材</a></p>
</div>
</body>
</html>

